package com.Kritsana_Ding.project_gps_demo;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity implements LocationListener{
	
	private TextView tvShow;
	private LocationManager lm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		tvShow = (TextView) findViewById(R.id.textViewShow);
		lm = (LocationManager) getSystemService(LOCATION_SERVICE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onLocationChanged(Location loc) {
		// TODO Auto-generated method stub
		try {
			if(loc == null){
				tvShow.setText("Error\n");
			}
			else{
				String lat = Double.toString(loc.getLatitude());
				String lon = Double.toString(loc.getLongitude());
				String spe = Double.toString(loc.getSpeed());
				tvShow.append("Lat : " + lat + "\n" +
						      "Lon : " + lon + "\n" +
						      "Spe : " + spe + "\n\n");
				
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		lm.removeUpdates(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, this);
	}
	
	

}
